package util;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

import demo.metrics.CountMapToDouble;
import io.micrometer.core.instrument.Metrics;

public abstract class MetricsUtil {

	public static final AtomicLong statusInvokeCount = new AtomicLong();

	private static final Map<String, AtomicLong> count = new ConcurrentHashMap<>();

	static {
		String[] keys = { "clearcount", "countmap", "jmxbeans", "jmx", "lazyBean" };
		for (String key : keys) {
			// 泛型类型不要使用 Object ，否则会显示 NaN
			Metrics.gauge(key, key, new CountMapToDouble(key));
			// Gauge.builder(key, key, new
			// CountMapToDouble(key)).register(meterRegistry);
		}
		
		System.out.println("MetricsUtil init done,"+LocalDateTime.now());

	}

	public static void increment(String key) {
		if (key == null) {
			return;
		}
		AtomicLong al = count.get(key);
		if (al == null) {
			al = new AtomicLong();
			count.put(key, al);
		}
		al.incrementAndGet();
	}

	public static Long get(String key) {
		AtomicLong al = count.get(key);
		if (al == null) {
			return null;
		}
		return al.get();
	}

	public static void clear() {
		count.clear();
	}

	public static Map<String, AtomicLong> getCountMap() {
		return count;
	}
}